package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.CarInfo;
import com.ruoyi.system.domain.DriverScore;
import com.ruoyi.system.domain.TravelStatus;
import com.ruoyi.system.service.ICarInfoService;
import com.ruoyi.system.service.IDriverScoreService;
import com.ruoyi.system.service.ITravelStatusService;
import com.ruoyi.system.utils.PasswordEncode;
import com.ruoyi.system.vo.DriverVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.mapper.DriverMapper;
import com.ruoyi.system.domain.Driver;
import com.ruoyi.system.service.IDriverService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 司机管理Service业务层处理
 *
 * @author trip
 * @date 2024-09-10
 */
@Service
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements IDriverService {

    @Autowired
    private DriverMapper driverMapper;
    @Autowired
    private IDriverScoreService driverScoreService;
    @Autowired
    private ITravelStatusService travelStatusService;
    @Autowired
    private ICarInfoService carInfoService;

    /**
     * 查询司机管理
     *
     * @param driverId 司机管理主键
     * @return 司机管理
     */
    @Override
    public Driver selectDriverByDriverId(Long driverId) {
        return driverMapper.selectDriverByDriverId(driverId);
    }

    /**
     * 查询司机管理列表
     *
     * @param driver 司机管理
     * @return 司机管理
     */
    @Override
    public List<Driver> selectDriverList(Driver driver) {
        return driverMapper.selectDriverList(driver);
    }

    /**
     * 新增司机管理
     *
     * @param driver 司机管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDriver(Driver driver) {
                driver.setCreateTime(DateUtils.getNowDate());
        int num = 0;
        try{
            String password = driver.getPassword();
            if (StrUtil.isNotBlank(password)) {
                driver.setPassword(PasswordEncode.encode(password));
            }
            //新增司机
            num=driverMapper.insertDriver(driver);
            if (num <=0 ) return num;
            //新增司机行为分数和接单状态
            Long driverId = driver.getDriverId();
            DriverScore driverScore = new DriverScore();
            driverScore.setDelFlag("0");
            driverScore.setDriverId(driverId);
            driverScore.setDriverScore(100);
            driverScoreService.insertDriverScore(driverScore);
            TravelStatus travelStatus = new TravelStatus();
            travelStatus.setDelFlag("0");
            travelStatus.setDriverId(driverId);
            travelStatus.setStatus("0");
            travelStatusService.insertTravelStatus(travelStatus);
            return num;
        }catch (Exception e){
            throw new RuntimeException("Failed to insert driver", e);
        }
    }

    /**
     * 修改司机管理
     *
     * @param driver 司机管理
     * @return 结果
     */
    @Override
    public int updateDriver(Driver driver) {
                driver.setUpdateTime(DateUtils.getNowDate());
        String password = driver.getPassword();
        if (StrUtil.isNotBlank(password)) {
            driver.setPassword(PasswordEncode.encode(password));
        }
        return driverMapper.updateDriver(driver);
    }


    /**
     * 批量删除司机管理
     *
     * @param driverIds 需要删除的司机管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDriverByDriverIds(Long[] driverIds) {
        if (driverIds == null || driverIds.length == 0 ) {
            return 0;
        }

        // 批量逻辑删除司机
        LambdaUpdateWrapper<Driver> driverWrapper = Wrappers.lambdaUpdate(Driver.class)
                .in(Driver::getDriverId, driverIds)
                .set(Driver::getDelFlag, "1");
        super.update(null, driverWrapper);

        // 批量逻辑删除司机行为分数
        LambdaUpdateWrapper<DriverScore> driverScoreWrapper = Wrappers.lambdaUpdate(DriverScore.class)
                .in(DriverScore::getDriverId, driverIds)
                .set(DriverScore::getDelFlag, "1");
        driverScoreService.update(null, driverScoreWrapper);

        // 批量逻辑删除出行状态
        LambdaUpdateWrapper<TravelStatus> travelStatusWrapper = Wrappers.lambdaUpdate(TravelStatus.class)
                .in(TravelStatus::getDriverId, driverIds)
                .set(TravelStatus::getDelFlag, "1");
        travelStatusService.update(null, travelStatusWrapper);

        //批量逻辑删除所属车辆
        LambdaUpdateWrapper<CarInfo> carInfoWrapper = Wrappers.lambdaUpdate(CarInfo.class)
                .in(CarInfo::getDriverId, driverIds)
                .set(CarInfo::getDelFlag, "1");
        carInfoService.update(null, carInfoWrapper);

        return 1;
    }

    /**
     * 删除司机管理信息
     *
     * @param driverId 司机管理主键
     * @return 结果
     */
    @Override
    public int deleteDriverByDriverId(Long driverId) {
        return driverMapper.deleteDriverByDriverId(driverId);
    }


    /**
     * 查询司机列表(修改)
     * @param driver
     * @return
     */
    @Override
    public List<DriverVO> getDriverList(Driver driver) {
        return driverMapper.getDriverList(driver);
    }

}



